/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "Wed_thread.h"

/*******************************************************************************
 * Code
 ******************************************************************************/
#if WED_MEM_DYMANICS
/**
 * \brief 线程动态创建
 *
 * @param p_thread_func     线程运作函数
 * @param p_thread_func_arg 线程运作函数参数
 * @param p_thread_name     线程名字
 * @param thread_stack_size 线程栈大小
 * @param thread_Priority   线程优先级
 *
 * @return 成功返回线程结构体
 */
struct Wed_thread *Wed_thread_create(Wed_thread_func_t  p_thread_func,
                                     void              *p_thread_func_arg,
                                     const char        *p_thread_name,
                                     uint32_t           thread_stack_size,
                                     uint32_t           thread_Priority){
    struct Wed_thread *p_thread = NULL;
#if WED_KERNEL_FREERTOS
    BaseType_t         ret;
#endif

    if ((p_thread_func == NULL) ||
            (p_thread_name == NULL)
#if WED_KERNEL_FREERTOS
            || (thread_Priority >= configMAX_PRIORITIES)
#endif
            ) {
        return NULL;
    }

    p_thread = Wed_calloc(1, sizeof(struct Wed_thread));
    if (p_thread == NULL) {
        return NULL;
    }
#if WED_KERNEL_FREERTOS
    ret = xTaskCreate(p_thread_func,
                      p_thread_name,
                      thread_stack_size / sizeof(StackType_t),
                      p_thread_func_arg,
                      configMAX_PRIORITIES - 1 - thread_Priority,
                     &p_thread->p_task_handle);
    if (ret == pdPASS) {
        return p_thread;
    }

    Wed_free(p_thread);
#endif
    WED_ERR_PRINT("thread \"%s\" create failed", p_thread_name);
    return NULL;
}
#endif

/**
 * \brief 线程初始化
 *
 * @param p_thread_func     线程运作函数
 * @param p_thread_func_arg 线程运作函数参数
 * @param p_thread_name     线程名字
 * @param thread_stack_size 线程栈大小
 * @param p_thread_stack    线程栈缓存
 * @param thread_Priority   线程优先级
 * @param p_thread          线程结构体
 *
 * @return 成功返回 0
 */
int Wed_thread_init(Wed_thread_func_t  p_thread_func,
                    void              *p_thread_func_arg,
                    const char        *p_thread_name,
                    uint32_t           thread_stack_size,
                    uint8_t           *p_thread_stack,
                    uint32_t           thread_Priority,
                    struct Wed_thread *p_thread){
#if WED_KERNEL_FREERTOS
    TaskHandle_t p_handle;

    p_handle = xTaskCreateStatic(p_thread_func,
                                 p_thread_name,
                                 thread_stack_size / sizeof(StackType_t),
                                 p_thread_func_arg,
                                 configMAX_PRIORITIES - 1 - thread_Priority,
                                (StackType_t *)p_thread_stack,
                                (StaticTask_t *)&p_thread->task_handle);
    if (p_handle == NULL) {
        return -WED_ERROR;
    }
#endif
    return 0;
}
